<template>
  <div class="search">
    <div class="search-input-wrapper">
      <SearchInput v-model="query"></SearchInput>
    </div>
    <Scroll class="search-content" v-show="!query">
      <div>
        <div class="hot-keys">
          <h1 class="title">热门搜索</h1>
          <ul>
            <li
              class="item"
              v-for="item in hotKeys"
              :key="item.id"
              @click="addQuery(item.key)"
            >
              <span>{{ item.key }}</span>
            </li>
          </ul>
        </div>
        <div class="search-history" v-show="searches.length">
          <h1 class="title">
            <span class="text">搜索历史</span>
            <span class="clear" @click="showConfirm">
              <i class="icon-clear"></i>
            </span>
          </h1>
          <Confirm
            ref="confirmref"
            text="你确定清除搜索历史吗"
            @confirm="confirmf"
            @cancel="cancelf"
          ></Confirm>
          <SearchList
            :searches="searches"
            @select="selecthisthory"
            @delete="deletehistory"
          ></SearchList>
        </div>
      </div>
    </Scroll>
    <div class="search-result" v-show="query">
      <Suggest
        @selectSinger="selectSinger"
        @selectSong="selectSong"
        :query="query"
      ></Suggest>
    </div>
  </div>
  <RouterView v-slot="{ Component }">
    <Transition name="slider">
      <component :is="Component" :singer="singer"></component>
    </Transition>
  </RouterView>
</template>

<script>
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
import SearchInput from '@/components/base/search/search-input.vue'
import { getHotKeys } from '@/services/search'
import Suggest from '@/components/suggest/suggest.vue'
import SearchList from '@/components/search-list/search-list.vue'
import storage from 'good-storage'
import Scroll from '@/components/base/scroll/scroll.vue'
import Confirm from '@/components/base/confirm/confirm.vue'
export default {
  name: 'Search',
  components: { SearchInput, Suggest, Scroll, SearchList, Confirm },
  setup () {
    const query = ref('')
    const hotKeys = ref([])
    const router = useRouter()
    const store = useStore()
    const singer = ref({})
    const confirmref = ref(null)
    const searches = computed(() => store.state.searchHistory)
    getHotKeys().then((res) => {
      hotKeys.value = res.hotKeys
    })
    function addQuery (key) {
      query.value = key
    }
    function selectSinger (Singer) {
      singer.value = Singer
      storage.session.set('__singer__', Singer)
      router.push(`/search/${Singer.mid}`)
      store.commit('addhistory', query.value)
    }
    function selectSong (Song) {
      store.commit('addhistory', query.value)
      store.dispatch('addsong', Song)
    }
    function selecthisthory (item) {
      query.value = item
    }
    function deletehistory (item) {
      store.commit('deleteonehis', item)
    }
    function showConfirm () {
      confirmref.value.show()
    }
    function confirmf () {
      store.commit('clearhistory')
      confirmref.value.hide()
    }
    function cancelf () {
      confirmref.value.hide()
    }
    return {
      query,
      hotKeys,
      singer,
      addQuery,
      selectSinger,
      selectSong,
      searches,
      confirmref,
      selecthisthory,
      deletehistory,
      showConfirm,
      confirmf,
      cancelf
    }
  }
}
</script>

<style scoped lang="scss">
.search {
  position: fixed;
  width: 100%;
  top: 88px;
  bottom: 0;
  display: flex;
  flex-direction: column;
  .search-input-wrapper {
    margin: 20px;
  }
  .search-content {
    flex: 1;
    overflow: hidden;
    margin-top: 10px;
    .hot-keys {
      margin: 0 20px 20px 20px;
      .title {
        margin-bottom: 20px;
        font-size: $font-size-medium;
        color: $color-text-l;
      }
      .item {
        display: inline-block;
        padding: 5px 10px;
        margin: 0 20px 10px 0;
        border-radius: 6px;
        background: $color-highlight-background;
        font-size: $font-size-medium;
        color: $color-text-d;
      }
    }
    .search-history {
      position: relative;
      margin: 0 20px;
      .title {
        display: flex;
        align-items: center;
        height: 40px;
        font-size: $font-size-medium;
        color: $color-text-l;
        .text {
          flex: 1;
        }
        .clear {
          @include extend-click();
          .icon-clear {
            font-size: $font-size-medium;
            color: $color-text-d;
          }
        }
      }
    }
  }
  .search-result {
    flex: 1;
    overflow: hidden;
  }
}
</style>
